package com.zw.mes.module.proStrucEdit.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.proStrucEdit.model.ProStrucEditProjectModel;
import com.zw.mes.module.proStrucEdit.model.SpecialModel;
import com.zw.mes.module.proStrucEdit.model.VwDsnProStrucModel;
import com.zw.mes.module.proStrucEdit.service.ProStrucEditService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 徐一贺
 * @className ProStrucEditController
 * @description 产品结构编辑控制层
 * @date 2021/3/11 15:14
 * @versron ver 1.0
 */
@Controller
@RequestMapping("/proStrucEdit")
public class ProStrucEditController extends BaseController {
    @Autowired
    private ProStrucEditService service;

    /**
     * 页面跳转到产品结构编辑列表页
     *
     * @return
     */
    @GetMapping
    public String page(Model model) {
//        List<SpecialModel> specialModelList = service.findSpecialList();
//        model.addAttribute("specialList",specialModelList);
//        String specialIds = "";
//        for (SpecialModel specialModel:specialModelList) {
//            specialIds+=specialModel.getSpecialId()+",";
//        }
//        specialIds=specialIds.substring(0,specialIds.length()-1);
//        model.addAttribute("specialIds",specialIds);
        return "prodTaskManager/proStrucEdit/page";
    }

    /**
     * 页面跳转到产品结构编辑页
     *
     * @return
     */
    @GetMapping("/gotoEdit/{proId}")
    @RequiresPermissions("pro:proStrucEdit:edit")
    public String gotoEditpage(@PathVariable("proId") Integer proId, Model model) {
        List<SpecialModel> specialModelList = service.findSpecialList();
        model.addAttribute("specialList", specialModelList);
        String specialIds = "";
        for (SpecialModel specialModel : specialModelList) {
            specialIds += specialModel.getSpecialId() + ",";
        }
        specialIds = specialIds.substring(0, specialIds.length() - 1);
        model.addAttribute("specialIds", specialIds);
        model.addAttribute("project", service.getVwProjectByPrimaryKey(proId));
        return "prodTaskManager/proStrucEdit/edit/page";
    }

    /**
     * 加载可编辑非设计类项目列表
     */
    @ResponseBody
    @PostMapping("/proList")
    @RequiresPermissions("pro:proStrucEdit:view")
    public JsonResult proStrucEditProjectList(ProStrucEditProjectModel param) {
        startPage();
        List<ProStrucEditProjectModel> list = service.findProStrucEditProjectList(param);
        return jsonResult(list);
    }

    /**
     * 加载可编辑非设计类项目的产品结构列表
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("pro:proStrucEdit:view")
    public JsonResult proStrucEditList(VwDsnProStrucModel param) {
        startPage();
        List<VwDsnProStrucModel> list = service.findProStrucEditList(param);
        return jsonResult(list);
    }

    /**
     * 提交
     *
     * @param idStr
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions("pro:proStrucEdit:edit")
    public JsonResult submit(String idStr) {
        return service.submit(idStr);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("pro:proStrucEdit:del")
    public JsonResult submit(Integer id) {
        return service.remove(id);
    }

    /**
     * 保存添加/编辑
     *
     * @param vwDsnProStrucModel 保存的对象
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("pro:proStrucEdit:edit")
    public JsonResult save(VwDsnProStrucModel vwDsnProStrucModel) {
        return service.save(vwDsnProStrucModel);
    }

    /**
     * 根据proId和proPart判断该产品结构是否存在
     *
     * @param vwDsnProStrucModel
     * @return
     */
    @ResponseBody
    @PostMapping("/proPartExist")
    public String proPartExist(VwDsnProStrucModel vwDsnProStrucModel) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", service.proPartExist(vwDsnProStrucModel));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }
}
